/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mp.health.manager;

import com.mp.appApi.database.HibernateUtils;
import com.mp.appApi.util.FileUtils;
import com.mp.health.common.Constants;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @Date 130918
 * @author vannh
 */
public class AppSevletListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("---Initial connection pool");
        HibernateUtils hibernateUtils = HibernateUtils.getInstance();
        ServletContext sContext = sce.getServletContext();
        String configDBPath = sContext.getInitParameter(Constants.PARAM_CONFIG_DB);
        String fullPathConfig = FileUtils.getFullPath(configDBPath);
        
        try {
            hibernateUtils.buildPool(fullPathConfig, Constants.SALT);
            System.out.println("---Initial connection pool success");
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("---Destroy connection pool");
        HibernateUtils hibernateUtils = HibernateUtils.getInstance();
        
        try {
            hibernateUtils.releaseConnection();
            System.out.println("---Destroy connection pool success");
        } catch (Exception ex) {
            System.out.println("---Destroy connection pool fail");
        }
    }
    
}